from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
import time

driver = webdriver.Chrome()
driver.maximize_window()

driver.get(r"file:///C:/TT/%E4%B9%A0%E7%9F%A5/%E8%87%AA%E5%8A%A8%E5%8C%96%E4%BB%A3%E7%A0%81-4%E6%9C%9F/studyDemo01/selenium(%E4%B8%8B)/selectDemo.html")
# 通过定位到的下拉框元素实例化一个下拉框对象
ele = driver.find_element(By.XPATH, "//select")
slect_obj = Select(ele)

time.sleep(3)
# select_by_index()			通过索引选择，index 索引从 0 开始
slect_obj.select_by_index(2)
time.sleep(2)
# select_by_value()			通过值选择(option标签的一个属性值)
slect_obj.select_by_value('shanghai')
time.sleep(2)
# select_by_visible_text()		通过文本选择(下拉框的值)
slect_obj.select_by_visible_text("成都")
# all_selected_options		查看所有已选，返回的是元素
print(slect_obj.all_selected_options)
# first_selected_option		查看第一个已选
print(slect_obj.first_selected_option)
# is_multiple				查看是否是多选
print(slect_obj.is_multiple)
# options					查看选项元素列表
print(slect_obj.options)
# 取消选择
# deselect_by_index()
slect_obj.deselect_by_index(6)
# deselect_by_value()
# deselect_by_visible_text()

time.sleep(5)
driver.quit()